<?php

function get_where($param){
 
    $where = array();
    $where_str = "";
	 if(isset($param['role_id']) && $param['role_id']){
        $where[] = "$5=={$param['role_id']}";
    }

    if(!empty($where)){
        $where_str = " && ".implode(" && ",$where);
    }
    return $where_str;
}

/**
 *  
 * @param $param
 */
//http://log.bajian.wan.sogou.com/index.php?a=get_coin_expend_total_log&server_id=10001
function get_coin_expend_total_log($param){

    $param['log_path'] = str_replace("*",date("d",$param['log_time']),$param['log_path']);
    if($param['coin_type']==2){
        $where_str = get_where($param);
        $cmd = <<<EOT
for i in `awk -F ',' '{print $8}' {$param['log_path']}/binding_ingot_expend_[0-9]* |sort -u `;do echo -en "\$i ";awk 'BEGIN {FS=",";count=0;ybsum=0};{if ($8 == '"\${i}"') { count=count+$9;ybsum=ybsum+$11}}; END {print count,ybsum}'  {$param['log_path']}/binding_ingot_expend_[0-9]* ;done
EOT;
        $data_ingot_expend  = exec_cmd($cmd,$param);
        $cmd2 = <<<EOT
for i in `awk -F ',' '{print $10}'  {$param['log_path']}/binding_ingot_expend_other_[0-9]* |sort -u`;do echo -en "\$i ";awk 'BEGIN {FS=",";count=0};{if ($10 == '"\${i}"') count=count+$8}; END {print count}'  {$param['log_path']}/binding_ingot_expend_other_[0-9]*;done
EOT;

        $data_ingot_expend_other = exec_cmd($cmd2,$param);
    }else{
        $where_str = get_where($param);
        $cmd = <<<EOT
 for i in `awk -F ',' '{print $8}'  {$param['log_path']}/ingot_expend_[0-9]* |sort -u `;do echo -en "\$i ";awk 'BEGIN {FS=",";count=0;ybsum=0};{if ($8 == '"\${i}"') { count=count+$9;ybsum=ybsum+$11}}; END {print count,ybsum}' {$param['log_path']}/ingot_expend_[0-9]* ;done
EOT;
        $data_ingot_expend  = exec_cmd($cmd,$param);
        $cmd2 = <<<EOT
for i in `awk -F ',' '{print $10}'  {$param['log_path']}/ingot_expend_other_[0-9]* |sort -u`;do echo -en "\$i ";awk 'BEGIN {FS=",";count=0};{if ($10 == '"\${i}"') count=count+$8}; END {print count}'  {$param['log_path']}/ingot_expend_other_[0-9]*;done
EOT;

        $data_ingot_expend_other = exec_cmd($cmd2,$param);
    }



//    print_r($data_ingot_expend);
//    print_r($data_ingot_expend_other);

  
/*
CREATE TABLE `s_coin_expend_total` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `platform_id` int(10) NOT NULL DEFAULT '0' COMMENT '平台id',
  `game_server_id` int(10) NOT NULL DEFAULT '0' COMMENT '区服id',
  `coin_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '货币类型 元宝1 绑元2 银两3 其它4',
  `expend_type` tinyint(4) NOT NULL DEFAULT '1' COMMENT '1购买消耗 2.其它消耗',
  `item_id` int(10) NOT NULL DEFAULT '0' COMMENT '道具id/用途',
  `ingot_cost` int(10) NOT NULL DEFAULT '0' COMMENT '元宝消耗',
  `addtime` int(10) NOT NULL DEFAULT '0' COMMENT '添加时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `p_g_c_e_u` (`platform_id`,`game_server_id`,`coin_type`,`expend_type`,`addtime`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='元宝去向分析';
*/
    $log = array();
    foreach($data_ingot_expend['data'] as $k=>$v){
        $log_arr = explode(" ",$v);
        $log[] = array(
			'coin_type'=>$param['coin_type']==2?2:1,
            'expend_type'=>1,
            'item_id'=>$log_arr[0],
            'item_num'=>$log_arr[1],
            'ingot_cost'=>$log_arr[2],
		);
    }

    foreach($data_ingot_expend_other['data'] as $k=>$v){
        $log_arr = explode(" ",$v);
        $log[] = array(
            'coin_type'=>$param['coin_type']==2?2:1,
            'expend_type'=>2,
            'item_id'=>$log_arr[0],
            'ingot_cost'=>$log_arr[1],
            //'item_num'=>0,
        );
    }
    $data['total'] = count($log);
    ajax_return($data['total'],$data['per_page'],$data['total_page'],$data['page'],$log);
}